package handler

import (
	"encoding/json"
	"role-guard/cache"
	"role-guard/modler"
	"role-guard/pkg"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func Add(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"mas":  "不能为空",
			"code": 500,
		})
		return
	}

	redis_add_ley := "redis_key" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_add_ley).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"mas":  "密码错误3次",
			"code": 500,
		})
		return
	}

	var user modler.User
	err := modler.DB.Where("username=?", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"mas":  "查询失败",
			"code": 500,
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"mas":  "用户为空",
			"code": 500,
		})
		return
	}

	if user.Password != pkg.MD5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_add_ley)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_add_ley, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"mas":  "密码错误3次，封禁2小时",
				"code": 500,
			})
			return
		}

		c.JSON(200, gin.H{
			"mas":  "密码错误",
			"code": 500,
		})
		return
	}

	c.JSON(200, gin.H{
		"mas":  "登录成功",
		"code": 200,
		"data": user,
	})
	return

}

func UserRode(c *gin.Context) {
	uid := c.Query("uid")
	/*
		SELECT
		  *
		FROM
		  rodes
		  LEFT JOIN user_rodes ON user_rodes.rid = rodes.id
		WHERE
		  user_rodes.uid =1
	*/
	var user []modler.Rode

	modler.DB.Table("rodes").
		Joins("LEFT JOIN user_rodes ON user_rodes.rid = rodes.id").
		Where("user_rodes.uid = ?", uid).
		Find(&user)
	c.JSON(200, gin.H{
		"mas":  "成功",
		"code": 200,
		"data": user,
	})
	return
}

func RodeNode(c *gin.Context) {
	rid := c.Query("rid")
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN rode_nodes ON rode_nodes.nid = nodes.id
		WHERE
		  rode_nodes.rid =1
	*/

	var data []modler.Node
	modler.DB.Table("nodes").
		Joins("LEFT JOIN rode_nodes ON rode_nodes.nid = nodes.id").
		Where("rode_nodes.rid = ?", rid).
		Find(&data)
	c.JSON(200, gin.H{
		"mas":  "成功",
		"code": 200,
		"data": data,
	})
	return

}

func RodeAdd(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))

	data := modler.UserRode{
		Uid: uid,
		Rid: rid,
	}
	modler.DB.Create(&data)
	c.JSON(200, gin.H{
		"mas":  "成功",
		"code": 200,
	})
	return

}

func Ads(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	user := modler.User{
		Username: username,
		Password: password,
	}
	modler.DB.Create(&user)
	c.JSON(200, gin.H{
		"mas":  "成功",
		"code": 200,
		"data": user,
	})
	return
}

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var data []modler.User
	redis_list_ley := "list_err" + strconv.Itoa(page)
	result, _ := cache.Rdb.Get(cache.Ctx, redis_list_ley).Result()
	if result == "" {
		modler.DB.Offset(offset).Limit(size).Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_list_ley, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &data)
	}

	var total int64

	modler.DB.Model(&modler.User{}).Count(&total)

	c.JSON(200, gin.H{
		"mas":   "成功",
		"code":  200,
		"data":  data,
		"total": total,
	})
}

func Login(c *gin.Context) {
	id := c.Query("id")

	var data modler.User

	redis_list_ley := "list_err_id" + id
	result, _ := cache.Rdb.Get(cache.Ctx, redis_list_ley).Result()
	if result == "" {
		modler.DB.Where("id=?", id).First(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_list_ley, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &data)
	}
	
	c.JSON(200, gin.H{
		"mas":  "搜索成功",
		"code": 200,
		"data": data,
	})
}
